package com.trantor.stockmarketwatch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class StockPriceCommands {
	
	private static String restUrl = "http://www.google.com/ig/api?stock=";
	
	private List<String> commands = new ArrayList<String>();
	private List<String> results = new ArrayList<String>();
	
	public List<String> run(String[] stocks) {
		for(String s:stocks) {
			commands.add(restUrl + s);
		}		
		for(String c:commands) {
			results.add(getStockPrice(c));
		}
		return results;
	}
	
	private String getStockPrice(String command) {
		String result = "";
		
		try {
			
			URL yahoo = new URL(command);
			BufferedReader in = new BufferedReader(
			            new InputStreamReader(
			            yahoo.openStream()));

			String inputLine;

			while ((inputLine = in.readLine()) != null)
			    result += inputLine;

			in.close();
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}
